using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class ShoppingCart : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["ID"] == null || "".Equals(Session["ID"].ToString()))
        {
            Response.Redirect("DangNhap.aspx");
        }
        lblError.Visible = false;
        string ma = Request["maSanPham"] as string;
        if (Session["Counter"] != null && "".Equals(Session["Counter"].ToString()))
        {
            int couter = Int16.Parse(Session["Counter"].ToString());
            couter ++;
            Session["Counter"] = couter.ToString();
        }
         if(!Page.IsPostBack)
        {
            Session["Counter"] = 1;

            ShoppingCartEntity entity = GetProduct(ma);
            if (entity == null) return;
            lblMaSanPham.Text = ma;
            lblTenSanhPham.Text = entity.Name;
            txtSoLuong.Text = "1";
            //entity.ShoppingDate = DateTime.Now;
            //entity.CustomerId = 0; // HttpContext.Current.User.Identity;
            //ShoppingCartBO cartBO = new ShoppingCartBO();
            //cartBO.ShoppingCartInsert(entity);
        }
    }

    public ShoppingCartEntity GetProduct(string id)
    {
        ShoppingCartEntity entity = new ShoppingCartEntity();

        FastFoodBO foodBO = new FastFoodBO();
        DataTable result = foodBO.GetById(id);
        if (result.Rows.Count == 0)
        {
            return null;
        }
        else
        {
            long foodId;
            long.TryParse(id, out foodId);
            entity.FoodId = foodId;
            entity.Image = result.Rows[0]["Image"].ToString();
            entity.Name = result.Rows[0]["Name"].ToString();
            try
            {
                entity.Unit = long.Parse(result.Rows[0]["Unit"].ToString());
            }
            catch {
                entity.Unit = 0;
            }
            try
            {
            entity.Cost = long.Parse(result.Rows[0]["Cost"].ToString());
            }catch{
                entity.Cost=0;
            }
        }
        return entity;
    }
    protected void addToGioHang_Click(object sender, EventArgs e)
    {
        int soLuong = 0;
        try{
            soLuong = Int16.Parse(txtSoLuong.Text.Trim());
            if (soLuong <= 0)
            {
                txtSoLuong.Focus();
                lblError.Visible = true;
                return;
            }
        }catch{
            txtSoLuong.Focus();
            lblError.Visible = true;
            return;}

        if (Session["tableGioHang"] == null)
        {
            gioHang GioHang = new gioHang();
            Session["tableGioHang"] = GioHang.CreateNewGioHang();
        }
        DataTable tbGioHang = (DataTable)Session["tableGioHang"];
        if (tbGioHang.Rows.Count > 0 && tbGioHang.Select("MaSanPham=" + lblMaSanPham.Text).Length > 0)
        {
            DataRow[] Dong = tbGioHang.Select("MaSanPham=" + lblMaSanPham.Text);
            int soLuongCu = 0;
            if(Dong[0]["SoLuong"]!=null)
                soLuongCu = Int16.Parse(Dong[0]["SoLuong"].ToString());
            soLuongCu += Int16.Parse(txtSoLuong.Text);
            Dong[0]["SoLuong"] = soLuongCu;
            Session["tableGioHang"] = tbGioHang;
            return;
        }
        string ma = Request["maSanPham"] as string;
        ShoppingCartEntity entity = GetProduct(ma);
            if (entity == null) return;

        DataRow row = tbGioHang.NewRow();
        row["MaSanPham"] = entity.FoodId;
        row["TenSanPham"] = entity.Name;
        row["SoLuong"] = soLuong;
        row["DonGia"] = entity.Cost;
        row["NgayDat"] = entity.ShoppingDate.ToShortDateString();
        row["Image"] = entity.Image;

        tbGioHang.Rows.Add(row);
        Session["tableGioHang"] = tbGioHang;
    }    
}
